import 'package:flutter/material.dart';
import 'package:flutter_app/customwidget/base_widget/BaseListWidget.dart';
import 'package:flutter_app/customwidget/scroll_widget/ScrollListWidget.dart';
import 'package:flutter_app/util/DataUtil.dart';

import 'container_widget/ContainerListWidget.dart';
import 'layout_widget/LayoutListWidget.dart';

/**
 * listview列表
 */
class ListWidget extends StatelessWidget {
  ListWidget({
    Key ? key,
    @required this.text, // 接收一个text参数
  }) : super(key: key);
  final String ?text;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(text??''),
      ),
      body: ListPage(),
    );
  }
}

class ListPage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<ListPage> {
  var list = DataUtil.getTitle();

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: list.length,
      itemBuilder: _buildItem,
      itemExtent: 60,
    );
  }
}

Widget _buildItem(BuildContext context, int index) {
  var list = DataUtil.getTitle();
  return Container(
    height: 60,
    color: Colors.cyan,
    child: GestureDetector(
      child: Container(
        margin: EdgeInsets.all(10),
        padding: EdgeInsets.all(10),
        color: Colors.yellow,
        child: Text(list[index]),
      ),
      onTap: () {
        toArount(index, context);
      },
    ),
  );
}

void toArount(int index, BuildContext context) {
  switch (index) {
    case 0:
      goTo(context, BaseListWidget(text: "基础组件"));
      break;
    case 1:
      goTo(context, LayoutListWidget(text: "布局类组件"));
      break;
    case 2:
      goTo(context, ContainerListWidget(text: "容器类组件"));
      break;
    case 3:
      goTo(context, ScrollListWidget(text: "滚动类组件"));
      break;
  }
}

void goTo(BuildContext context, Widget widget) {
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return widget;
  }));
}
